package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.BitmapDetail;

import java.util.List;

/**
 * Created by Aaron on 2015/7/22.
 */
public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    private List<BitmapDetail> bitmapsDetail;
    private boolean isFull;

    public ImageAdapter(Context ctx, List<BitmapDetail> bitmapsDetail) {
        this.ctx = ctx;
        this.bitmapsDetail = bitmapsDetail;
    }

    public void setBitmapsDetail(List<BitmapDetail> bitmapsDetail) {
        this.bitmapsDetail = bitmapsDetail;
    }

    public List<BitmapDetail> getBitmapsDetail() {
        return bitmapsDetail;
    }

    @Override
    public int getCount() {
        return bitmapsDetail.size();
    }

    public void setFull(boolean isFull) {
        this.isFull = isFull;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder=null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_image, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.iv_delete = (ImageButton) convertView.findViewById(R.id.iv_delete);
            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        viewHolder.iv_delete.setVisibility(View.INVISIBLE);

        viewHolder.iv_image.setImageBitmap(bitmapsDetail.get(position).bitmap);

        if (bitmapsDetail.size() < 9) {

            if (position == bitmapsDetail.size()-1) { //最后一个, 不显示红点
                viewHolder.iv_delete.setVisibility(View.INVISIBLE);
            }else {
                viewHolder.iv_delete.setVisibility(View.VISIBLE);
            }
        }else {
            //最后一个
            viewHolder.iv_delete.setVisibility(isFull ? View.INVISIBLE : View.VISIBLE);
        }

        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除照片
                bitmapsDetail.remove(position);
                notifyDataSetChanged();

//                //照片选择的 集合中的图片也删除掉
//                MyAdapter.mSelectedImage.remove(bitmapsDetail.get(position).mPath);
//                LogUtil.e("TAG", "移除的图片的地址==>" + bitmapsDetail.get(position).mPath);

                if (bitmapsDetail.size()==8) {

                }

            }
        });

        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_image;
        private ImageButton iv_delete;
    }

}
